1 using UnityEngine;
2 using
System.Collections;
3
4 namespace
Completed
5 {
6     
public class SoundManager : MonoBehaviour
7     {
8         
public AudioSource efxSource; //Drag a reference to the audio source which will play the sound effects.
9         
public AudioSource musicSource; //Drag a reference to the audio source which will play the music.
10         
public static SoundManager instance = null; //Allows other scripts to call functions from SoundManager.
11         
public float lowPitchRange = .95f; //The lowest a sound effect will be randomly pitched.
12         
public float highPitchRange = 1.05f; //The highest a sound effect will be randomly pitched.
13         
14         
15         
void Awake ()
16         {
17             
//Check if there is already an instance of SoundManager
18             
if (instance == null)
19                 
//if not, set it to this.
20                 instance =
this;
21             
//If instance already exists:
22             
else if (instance != this)
23                 
//Destroy this, this enforces our singleton pattern so there can only be one instance of SoundManager.
24                 Destroy (gameObject);
25             
26             
//Set SoundManager to DontDestroyOnLoad so that it won't be destroyed when reloading our scene.
27             DontDestroyOnLoad (gameObject);
28         }
29         
30         
31         
//Used to play single sound clips.
32         
public void PlaySingle(AudioClip clip)
33         {
34             
//Set the clip of our efxSource audio source to the clip passed in as a parameter.
35             efxSource.clip = clip;
36             
37             
//Play the clip.
38             efxSource.Play ();
39         }
40         
41         
42         
//RandomizeSfx chooses randomly between various audio clips and slightly changes their pitch.
43         
public void RandomizeSfx (params AudioClip[] clips)
44         {
45             
//Generate a random number between 0 and the length of our array of clips passed in.
46             
int randomIndex = Random.Range(0, clips.Length);
47             
48             
//Choose a random pitch to play back our clip at between our high and low pitch ranges.
49             
float randomPitch = Random.Range(lowPitchRange, highPitchRange);
50             
51             
//Set the pitch of the audio source to the randomly chosen pitch.
52             efxSource.pitch = randomPitch;
53             
54             
//Set the clip to the clip at our randomly chosen index.
55             efxSource.clip = clips[randomIndex];
56             
57             
//Play the clip.
58             efxSource.Play();
59         }
60     }
61 }


public AudioSource efxSource; Drag a reference to the audio source which will play the sound effects.

public AudioSource musicSource; Drag a reference to the audio source which will play the music.

public static SoundManager instance = null; Allows other scripts to call functions from SoundManager.

public float lowPitchRange = .95f; The lowest a sound effect will be randomly pitched.

public float highPitchRange = 1.05f; The highest a sound effect will be randomly pitched.

Check if there is already an instance of SoundManager

if not, set it to this.

If instance already exists:

Destroy this, this enforces our singleton pattern so there can only be one instance of SoundManager.

Set SoundManager to DontDestroyOnLoad so that it won't be destroyed when reloading our scene.

Used to play single sound clips.

Set the clip of our efxSource audio source to the clip passed in as a parameter.

Play the clip.

RandomizeSfx chooses randomly between various audio clips and slightly changes their pitch.

Generate a random number between 0 and the length of our array of clips passed in.

Choose a random pitch to play back our clip at between our high and low pitch ranges.

Set the pitch of the audio source to the randomly chosen pitch.

Set the clip to the clip at our randomly chosen index.

Play the clip.




Trò chơi giống như Rogue 2D sử dụng Unity 28.440 lượt xem

Gõ tìm kiếm nhanh...